package com.zhaogang.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.dto.SaleGoodsDetailDTO;
import com.zhaogang.entity.Order;
import com.zhaogang.entity.UserAccount;
import com.zhaogang.entity.UserDetails;
import com.zhaogang.service.UserInfoService;

/**
* @author
* @data
* @version
* @parameter
* @return
*/
@Controller
@RequestMapping("userInfo")
public class UserInfoController {
	
	@Autowired
	private UserInfoService userInfoService;
	

	@RequestMapping("getUserInfo")
	public ModelAndView getUserInfo(HttpServletRequest request){
		ModelAndView view=new ModelAndView();
		//UserAccount userAccount=new UserAccount();
		HttpSession session=request.getSession();
		UserAccount userAccount=(UserAccount) session.getAttribute("userToken");
		// userAccount.setUserAccount("xiaoqiang");
		//userAccount.setNickname("yasuo");
		UserDetails userDetails=userInfoService.selectUserDetailsInfo(userAccount);
		if(userDetails!=null){
			view.addObject(userDetails);
		}
		view.addObject(userAccount);
		view.setViewName("/user/userCenter");
		return view;
	}
	
	@RequestMapping("updateUserInfo")
	public void updateUserInfo(HttpServletRequest request,HttpServletResponse respose){
		//封装用户账户信息
		HttpSession session= request.getSession();
		UserAccount user= (UserAccount) session.getAttribute("userToken");
		int userId=user.getId();
		//int userId=1;
		String userAccount=request.getParameter("userAccount");
		UserAccount account=new UserAccount();
		account.setId(userId);
		account.setUserAccount(userAccount);
		//封装用户详情信息
		//String detailsId=request.getParameter("detailsId");
		String realname=request.getParameter("realname");
		String gender=request.getParameter("gender");
		String nickname=request.getParameter("nickname");
		String phone=request.getParameter("phone");
		String address=request.getParameter("address");
		//String addAdress=request.getParameter("addAdress");
		UserDetails detail=new UserDetails();
		//detail.setId(Integer.parseInt(detailsId));
		detail.setRealname(realname);
		detail.setGender(gender);
		detail.setPhone(phone);
		detail.setAddress(address);
		detail.setNickname(nickname);
		detail.setUserAccountId(userId);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("userAccount", account);
		map.put("userDetails", detail);
		userInfoService.updateUserInfo(map);
		
	}
	
	@RequestMapping("getOrderInfo")
	@ResponseBody
	public List<Order> getOrderInfo(HttpSession session){
		UserAccount user=(UserAccount) session.getAttribute("userToken");
		if(user==null){
			return null;
		}
		List<Order> list=new ArrayList<Order>();
		list=userInfoService.selectOrderInfo(user);
		return list;
	}
	
	@RequestMapping("deleteOrder")
	@ResponseBody
	public int  deleteOrder(int id){
		int i=userInfoService.deleteOrder(id);
		System.out.println("删除记录："+i);
		return i;
	}

	
	@RequestMapping("getGoodsInfo")
	@ResponseBody
	public void getGoodsInfo(HttpSession session,int saleGoodsId){
		SaleGoodsDetailDTO dto=new SaleGoodsDetailDTO();
		dto=userInfoService.getSaleGoodsInfo(saleGoodsId);
		System.out.println(dto);
		session.setAttribute("dto", dto);
	}
	
	@ResponseBody
	@RequestMapping("/modifyPW")
	public int modifyPSW(String newpsw,String oldpsw,HttpSession session){
		List<String> list=new ArrayList<String>();
		UserAccount userAccount=(UserAccount) session.getAttribute("userToken");
		String account=userAccount.getUserAccount();
		System.out.println(newpsw);
		System.out.println(oldpsw);
		list.add(newpsw);
		list.add(oldpsw);
		list.add(account);
		int i=4;
		try {
			i = userInfoService.updateAccountPW(list);
			System.out.println(i);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	

}
